/*
 * @Author: liuxinyea
 * @Date: 2024-06-12 09:43:42
 * @LastEditors: liuxinye
 * @LastEditTime: 2024-12-27 21:31:36
 * @Description: 地图加载和管理
 */
import { __private, _decorator, BoxCollider2D, Color, Component, EPhysics2DDrawFlags, instantiate, Node, PhysicsSystem2D, PolygonCollider2D, Prefab, Script, Size, Sprite, SpriteFrame, TiledLayer, TiledMap, TiledObjectGroup, UITransform, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MapManager')
export class MapManager extends Component {
    map: TiledMap
    start() {
      // const transform = this.getComponent(UITransform)
      // transform.anchorPoint = new Vec2(0,0)
      // 处理物理图层
      PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
    EPhysics2DDrawFlags.Pair |
    EPhysics2DDrawFlags.CenterOfMass |
    EPhysics2DDrawFlags.Joint |
    EPhysics2DDrawFlags.Shape;
      this.initPycLayer()
    }
    initPycLayer(){
      const map = this.getComponent(TiledMap)
      const pyList: Array<__private._cocos_tiledmap_tiled_types__TMXObject> = map.getObjectGroup('Pythic').getObjects()
      console.log(pyList);
      // TODO 合并联系的物理块儿
      // const collider = this.addComponent(PolygonCollider2D)
      // collider.points = []
      pyList.forEach((item:any)=>{
        // 使用预制体创建碰撞体
        const pyNodeItem = new Node()
        // 获取UITransform组件用于设置碰撞体尺寸
        const transform = pyNodeItem.addComponent(UITransform);
        // 设置位置
        pyNodeItem.position = new Vec3(item.x,item.y,0)

        // 设置大小
        transform.setContentSize(item.width,item.height)
        pyNodeItem.addComponent(BoxCollider2D);
        // 获取BxCollider2D组件用于设置碰撞体尺寸
        const boxCollider = pyNodeItem.getComponent(BoxCollider2D)
        boxCollider.size = new Size(item.width,item.height)
        // 处理锚点导致的位置偏移,tileMap的锚点在左上
        boxCollider.offset = new Vec2(item.width/2,-item.height/2)
        // 添加到地图节点下
        pyNodeItem.setParent(this.node)
      })
    }

    update(deltaTime: number) {
        
    }
}


